package com.yd.controller.sys;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Users;
import com.yd.service.UsersService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;


@Controller
public class UsersControllerSys {

	@Resource
	private UsersService usersService;
	
	
	@RequestMapping("/sys/user/pass/up")
	@ResponseBody
	public String sys_user_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String ypass = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("ypass"))){ypass=request.getParameter("ypass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Users users = usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(ypass).equals(users.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		users.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!usersService.updateById(users)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/user/touxiang/up")
	@ResponseBody
	public String sys_user_touxiang_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String img = "";
		if (UtsReg.getStringKB(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringTitle(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setImg(img);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/user/sex/up")
	@ResponseBody
	public String sys_user_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String sex = "";
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("sex", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setSex(sex);
		if(!usersService.updateById(users)){
			jsonObject.put("sex", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/user/age/up")
	@ResponseBody
	public String sys_user_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String age = "";
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "性别 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("age", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setAge(age);
		if(!usersService.updateById(users)){
			jsonObject.put("age", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}

	@RequestMapping("/sys/user/nicheng/up")
	@ResponseBody
	public String sys_user_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String nicheng = "";
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("nicheng", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setNicheng(nicheng);
		if(!usersService.updateById(users)){
			jsonObject.put("nicheng", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/user/name/up")
	@ResponseBody
	public String sys_user_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String name = "";
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("name", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setName(name);
		if(!usersService.updateById(users)){
			jsonObject.put("name", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/user/email/up")
	@ResponseBody
	public String sys_user_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String uid = "";
		String email = "";
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("email", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setEmail(email);
		if(!usersService.updateById(users)){
			jsonObject.put("email", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping(value={"/sys/user/delbyuid"})
	@ResponseBody
	public String sys_user_delbyuid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String uid="";
		if(UtsReg.getIntXl(request.getParameter("uid"))){uid=request.getParameter("uid").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(usersService.selectCount(new EntityWrapper<Users>().eq("id", uid))<1){
			jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!usersService.deleteById(uid)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
}
